package cn.zcf.org.controller;

import cn.zcf.basic.exception.BusinessException;
import cn.zcf.basic.utils.JsonResult;
import cn.zcf.org.dto.LoginDto;
import cn.zcf.org.service.ILoginService;
import cn.zcf.user.dto.WxDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;


    @PostMapping("/account")
    public JsonResult accountLogin(@RequestBody LoginDto loginDto){
        try {
            //登录成功要返回token登录信息 - 代替session
            return loginService.accountLogin(loginDto);
        } catch (BusinessException e){
            e.printStackTrace();
            return JsonResult.me().setMsg("登录失败，"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统繁忙，请稍后重试!!!");
        }
    }
    /**
     * 微信登录接口
     * @param code
     * @return
     */
    @GetMapping("/wechat/{code}")
    public JsonResult wechatLogin(@PathVariable("code") String code){
        try {
            return loginService.wechatLogin(code);
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统异常，请稍后重试!!!");
        }
    }
    /**
     * 微信绑定
     * @param
     * @return
     */
    @PostMapping("/wechat/binder")
    public JsonResult binder(@RequestBody WxDto dto){
        try {
            // service直接返回一个jsonResoult
            return loginService.binder(dto);
        }catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络繁忙！");
        }
    }
    @GetMapping("/logout/{token}")
    public JsonResult logOut(@PathVariable("token") String token){
        try {
            return loginService.logOut(token);
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统异常，请稍后重试!!!");
        }
    }

}